package com.ts.api.module.common.controller;

import com.gitee.apanlh.util.valid.Assert;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.BizEnum;
import com.ts.api.common.redis.SmsRedisKey;
import com.ts.api.exp.BizException;
import com.ts.api.module.common.service.SmsService;
import com.ts.api.module.test.service.TestAccountWhitelistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 *  短信
 *
 *  @author Pan
 */
@RestController
@RequestMapping("/common/sms")
public class SmsController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private TestAccountWhitelistService testAccountWhitelistService;

    /**
     *  短信获取验证码
     *
     *  @return ResultVO
     */
    @GetMapping("/getCode")
    public ResultVO<Object> getCode(@RequestParam(required = false) String phone) {
        Assert.isNotEmptyThrows(phone, new BizException(BizEnum.EMPTY));

        //  白名单账户统一123456
        if (testAccountWhitelistService.validate(phone)) {
            SmsRedisKey.PHONE_CODE.setForMinutes(phone, "888888", 3L);
            return ResultVO.suc("发送成功");
        }

        smsService.sendCode(phone);
        return ResultVO.suc("发送成功");
    }
}
